/*
 *  Copyright (C) 2011 felixtheripper <felixtheripper_AT_gmail.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
/**
 *
 * @author felixtheripper
 *
 * email: felixtheripper_AT_gmail.com
 * ... please remove "_AT_" from the above string to get the right email address
 *
 */


package org.myorg.ctm.etk.gui.swg.display.dialog;

import java.awt.Component;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 *
 * 
 */

final public class DlgEViewSrcFileTxtSys extends DlgEViewSrcFileTxtAbs
{
    /**
	 *
	 */
	private static final long serialVersionUID = 1L;



	// ------
    // PUBLIC

    public boolean show(File fle)
    {


        String strSource = _loadSource(fle);

        if (strSource == null)
        {
            //MySystem.s_printOutError(this, strMethod, "failed");
            return false;
        }

        if (! super._show_(strSource, fle.getName()))
        {
            //MySystem.s_printOutError(this, strMethod, "failed");
            return false;
        }

        return true;
    }



    public DlgEViewSrcFileTxtSys(Component cmpFrameOwner, String strTitleApplication)
    {
        super(cmpFrameOwner, strTitleApplication);
    }



    // -------
    // PRIVATE

    private String _loadSource(File fle)
    {
	    //String strMethod = "_loadSource(fle)";

	    String strSourceCode = new String();
	    char[] chrsBuffer = new char[50000];

	    try
	    {
		    InputStreamReader isr = new InputStreamReader(new FileInputStream(fle));

		    int intNch;

		    while ((intNch = isr.read(chrsBuffer, 0, chrsBuffer.length)) != -1)
		    {
		        strSourceCode += new String(chrsBuffer, 0, intNch);
		    }

		    isr.close();
		    isr = null;
	    }

	    catch(Exception exc)
	    {
	        exc.printStackTrace();
		    //MySystem.s_printOutWarning(this, strMethod, "exc caught, failed to open file:" + fle.getAbsolutePath());
		    //return null;

		    return new String("failed to open file \n\n" + fle.getAbsolutePath());
	    }

	    return strSourceCode;
    }
}
